import { Injectable } from '@nestjs/common';
import * as bcrypt from 'bcrypt';


@Injectable()
export class BcryptService {
  /**
   * 用于哈希密码的盐
   */
  private static readonly SALT_ROUNDS: number = 12;

  /**
   * 对比检查密码
   * @param {string} rawStr 原始密码
   * @param {string} hashStr 哈希密码
   */
  static async compare(rawStr: string, hashStr: string) {
    return bcrypt.compare(rawStr, hashStr);
  }
  /**
   * 生成hash密码
   * @param {string} rawStr 原始密码
   * @param {string} salt 盐
   */
  static async hash(rawStr: string, salt?: string) {
    return bcrypt.hash(rawStr, salt || BcryptService.SALT_ROUNDS);
  }
}